import json

from app.model import mysql
from app.service import response as response_service
from app.service.response import format_navs
from app.service.search import index_master, search_master, designer_master_case, search_master_case
from app.service.user import format_avatar_url
from app.settings.config import LIMIT


async def masters(page, keyword, navids, current_user):
    """
    获取大师列表
    :param navids:
    :param keyword:
    :param page:
    :param current_user:
    :return:
    """
    limit: int = LIMIT
    offset: int = (page - 1) * limit
    search = {
        "keyword": keyword,
        "navs": navids,
        "offset": offset,
        "limit": limit,

    }
    search_result = await search_master(search)
    designer_id_list = await mysql.UserInfo.filter(is_master=True).values_list("id", flat=True)
    for item in designer_id_list:
        await index_master(item)
    response_list = []
    for item in search_result:
        result = {}
        user = await mysql.User.filter(id=item["id"]).first()
        user_info = await mysql.UserInfo.filter(id=item["id"]).first()
        result["id"] = user.id
        result["nickname"] = user.nickname
        result["avatar"] = format_avatar_url(user.avatar)
        result["country"] = user_info.country
        result["personalProfile"] = user_info.personal_profile
        response_list.append(result)

    nav_name = await format_navs(4)
    for item in nav_name:
        for nav in item:
            if int(nav["id"]) == 5 or int(nav["id"]) == 11:
                nav["isSelected"] = True
            else:
                nav["isSelected"] = False
    data = {
        "navs": nav_name,
        "list": response_list
    }
    return data


async def master_case(page, keyword, navids, current_user):
    """
    大师案例
    :param navids:
    :param keyword:
    :param page:
    :param current_user:
    :return:
    """
    limit: int = LIMIT
    offset: int = (page - 1) * limit
    search = {
        "keyword": keyword,
        "navs": navids,
        "offset": offset,
        "limit": limit,

    }
    # designer = await mysql.UserInfo.filter(is_master=True).all().values_list("id", flat=True)
    # media_id_list = await mysql.Media.filter(user_id__in=designer).values_list("id", flat=True)
    # for item in media_id_list:
    #     await designer_master_case(item)
    search_result = await search_master_case(search)
    response_info_list = []
    for item in search_result:
        result = {}
        result["id"] = item["id"]
        result["url"] = ""
        info = await response_service.response_media(item["id"])
        try:
            result["type"] = info["type"]
            result["media"] = info["media"]
            result["desc"] = info["desc"]
            if info["type"] == 5:
                result["url"] = info["url"]
        except Exception as e:
            result["type"] = 0
            result["media"] = response_service.format_media_info()
            result["desc"] = ""
            result["url"] = ""
        user = await mysql.User.filter(id=item["user_id"]).first()
        result["nickname"] = user.nickname,
        result["avatar"] = format_avatar_url(user.avatar),
        result["userId"] = user.id
        result["a2b"] = True if await mysql.Follow.filter(user_id=current_user.id,
                                                          follow_user_id=user.id).first() else False
        response_info_list.append(result)
    nav_name = await format_navs(4)
    for item in nav_name:
        for nav in item:
            if int(nav["id"]) == 5 or int(nav["id"]) == 11:
                nav["isSelected"] = True
            else:
                nav["isSelected"] = False
    data = {
        "navs": nav_name,
        "list": response_info_list,
    }

    return data


async def master_detail(master_id, current_user):
    """
    大师详情
    :param master_id:
    :param current_user:
    :return:
    """
    designer = await mysql.User.filter(id=master_id).first()
    designer_info = await mysql.UserInfo.filter(id=master_id).first()
    media = await mysql.Media.filter(user_id=designer.id).all()
    user = {
        "id": designer.id,
        "nickname": designer.nickname,
        "avatar": format_avatar_url(designer.avatar),
        "personalProfile": designer_info.personal_profile,
        "position": designer_info.position,
        "country": designer_info.country,
        "a2b": True if await mysql.Follow.filter(user_id=current_user.id,
                                                 follow_user_id=master_id).first() else False,
        "b2a": True if await mysql.Follow.filter(user_id=master_id,
                                                 follow_user_id=current_user.id).first() else False
    }
    response_list = []
    for item in media:
        result = {}
        result["id"] = item.id
        result["url"] = ""
        info = await response_service.response_media(item.id)
        try:
            result["type"] = info["type"]
            result["media"] = info["media"]
            result["desc"] = info["desc"]
            if info["type"] == 5:
                result["url"] = info["url"]
        except Exception as e:
            result["type"] = 0
            result["media"] = response_service.format_media_info()
            result["desc"] = ""
            result["url"] = ""
        response_list.append(result)

    data = {
        "user": user,
        "personalProfileImage": json.dumps(designer_info.personal_profile_image) if designer_info.personal_profile_image else [],
        "result": response_list
    }

    return data
